package com.edu.eduservice.controller;


import com.edu.commonutils.R;
import com.edu.eduservice.domain.EduTeacher;
import com.edu.eduservice.domain.QueryTeacher;
import com.edu.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-12
 */
//@CrossOrigin
@Api(description = "讲师管理接口")
@RestController
@RequestMapping("/eduservice/eduteacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "分页方法")
    //分页
    @PostMapping("/{number}/{pageSize}")
    @ResponseBody
    public R pageShow(@ApiParam(name = "teacherquery",value = "条件查询") @RequestBody QueryTeacher teacherquery,
                      @ApiParam(name = "number",value = "页数") @PathVariable(value = "number") Integer number,
                      @ApiParam(name = "pageSize",value = "页大小")  @PathVariable(value = "pageSize")Integer pageSize){

//        try {
//            int i=10/0;
//        }catch (Exception e){
//            try {
//                throw new MyException(20001,"执行了自定义异常");
//            } catch (MyException ex) {
//                throw new RuntimeException(ex);
//            }
//        }

        return eduTeacherService.pagwShowService(teacherquery,number,pageSize);
    }

    @ApiOperation(value = "根据编号返回讲师对象")
    // 根据id返回讲师对象
    @GetMapping("/{id}")
    @ResponseBody
    public R byId(@PathVariable("id") @ApiParam(value = "编号",name = "id") String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "查询所有")
    // 查询所有
    @GetMapping("/findAll")
    @ResponseBody
    public R list() {
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        return R.ok().data("teacherList", teacherList);
    }

    @ApiOperation(value = "新增")
    // 新增
    @PostMapping
    @ResponseBody
    public R save(@RequestBody @ApiParam(value = "编号",name = "teacher")EduTeacher teacher) {
        boolean flag = this.eduTeacherService.save(teacher);
        if(flag)
            return R.ok().message("新增成功");
        return R.error().message("新增失败");
    }

    @ApiOperation(value = "删除")
    // 删除
    @DeleteMapping("{id}")
    @ResponseBody
    public R del(@PathVariable("id") String id) {
        boolean flag = this.eduTeacherService.removeById(id);
        if(flag)
            return R.ok().message("删除成功");
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "修改")
    // 修改
    @PutMapping
    @ResponseBody
    public R update(@RequestBody EduTeacher teacher) {
        boolean flag = this.eduTeacherService.updateById(teacher);
        if(flag)
            return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }

}

